package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> getPage(EmpQueryParam param) {
        //先弄条件查询
        LambdaQueryWrapper<Emp> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(StrUtil.isNotBlank(param.getName()), Emp::getName, param.getName())
                .eq(ObjectUtil.isNotNull(param.getGender()), Emp::getGender, param.getGender())
                .between(ObjectUtil.isNotNull(param.getBegin()) && ObjectUtil.isNotNull(param.getEnd()), Emp::getEntryDate, param.getBegin(), param.getEnd());

        //分页
        Page<Emp> objectPage = Page.of(param.getPage(), param.getPageSize());
        Page<Emp> page = this.page(objectPage, objectLambdaQueryWrapper);//用于存储分页结果和查询的条件

        //排序和找dept的部门
        List<Emp> records = objectPage.getRecords();
        Set<Integer> collect = records.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> deptList = deptService.listByIds(collect);
        Map<Integer, String> collect1 = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));

        records.forEach(emp -> {
                    emp.setDeptName(collect1.getOrDefault(emp.getDeptId(), "x部门"));
                }
        );


        return new PageResult(Long.valueOf(records.size()),records);
    }

    /**
     * 新增
     * @param emp
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());

        save(emp);

        List<EmpExpr> exprList = emp.getExprList();

        exprList.forEach(expr->expr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);

    }


    /**
     *
     * 根据id查询
     */
    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);

         emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,id)));
        return emp;
    }

    /**
     * 修改员工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        // 调用service层方法修改员工信息
        // 根据id更新员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        // 根据id批量删除员工的工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));

        // 批量保存员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList != null && !exprList.isEmpty()){
            exprList.forEach(expr->expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);

    }


    /**
     * 批量删除员工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpsByIds(List<Integer> ids) {
        // 调用service层方法批量删除员工信息
        removeBatchByIds(ids);

        // 删除员工工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
